﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.Messaging;
using Jn698._45GWTool.ViewModels;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Jn698._45GWTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = ServiceLocator.Current.GetInstance<MainViewModel>();
            //this.CommType.Background = Brushes.LightGreen;
            this.messageShow.Document.Blocks.Clear();
            //var color = new Color() { R= 184, G=134,B=11 };
            //var brush = new SolidColorBrush(color);
            
            Messenger.Default.Register<string>(this, "SendMessage", async(s) => { await AddMessage(s, Brushes.Green); });
            Messenger.Default.Register<string>(this, "RecvMessage", async (s) => { await AddMessage(s, Brushes.Blue); });
            Messenger.Default.Register<string>(this, "AnalyMessage", async (s) => { await AddMessage(s, Brushes.Orange); });
            Messenger.Default.Register<string>(this, "StopMessage", async (s) => { await AddMessage(s, Brushes.Red); });
            Messenger.Default.Register<string>(this, "Info", async (s) => { await AddMessage(s, Brushes.Black); });

        }

       

        public async Task AddMessage(string msg, Brush brush)
        {

            await this.messageShow.Dispatcher.BeginInvoke(new Action(() =>
            {

                Run run = new Run();

                Paragraph paragraph = new Paragraph();
                var bbrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 184, 134, 11));
                if (brush == Brushes.Orange)
                {
                    run.Foreground = bbrush;
                }
                else
                {
                    run.Foreground = brush;
                }

                run.Text = msg;
                run.FontSize = 12;

                paragraph.Inlines.Add(run);

                paragraph.LineHeight = 1;

                if (brush == Brushes.Red)
                {

                    paragraph.FontWeight = FontWeights.DemiBold;

                }

                this.messageShow.Document.Blocks.Add(paragraph);

                this.messageShow.ScrollToEnd();

            }));

        }

        private void clearMessage_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(this.messageShow.Selection.Text);
            this.messageShow.Document.Blocks.Clear();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            EncryptorConfigView encryptorConfigView = new EncryptorConfigView();
            encryptorConfigView.ShowDialog();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            InfoView infoView = new InfoView();
            infoView.ShowDialog();
        }

        private void ShowTermialParamterView(object sender, RoutedEventArgs e)
        {
            TerminalParameterView terminalParameterView = new TerminalParameterView();
            terminalParameterView.ShowDialog();
        }
    }
}
